package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.DccBizException;
import com.csun.cmny.provider.model.domain.OmcExpenseItem;
import com.csun.cmny.provider.model.domain.OmcExpenseType;
import com.csun.cmny.provider.model.move.MoveOldmanIeType;
import com.csun.cmny.provider.model.move.MoveOldmanTypeIetype;
import com.csun.cmny.provider.service.OmcDataMoveFeighApi;
import com.csun.cmny.provider.model.domain.OmcExpenseTypeModelCode;
import com.csun.cmny.provider.service.OmcExpenseFeignApi;
import com.csun.cmny.provider.service.OmcRpcService;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-10 19:26
 * @Description:
 */
@Slf4j
@Service
public class OmcRpcServiceImpl implements OmcRpcService {

    @Resource
    private OmcExpenseFeignApi omcExpenseFeignApi;
    @Resource
    private OmcDataMoveFeighApi omcDataMoveFeighApi;

    /**
     * save omc expense type
     *
     * @param omcExpenseType the omc expense type
     * @return
     */
    @Override
    public Long saveOmcExpenseType(OmcExpenseType omcExpenseType) {

        try {
            Wrapper<Long> wrapper = omcExpenseFeignApi.saveOmcExpenseType(omcExpenseType);
            if (wrapper == null) {
                throw new DccBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DccBizException(ErrorCodeEnum.MDC10021002);
            }

            Long result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("saveOmcExpenseType = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * save expense type model code
     *
     * @param omcExpenseTypeModelCode the omc expense type model code
     *
     * @return
     */
    @Override
    public Long saveOmcExpenseTypeModelCode(OmcExpenseTypeModelCode omcExpenseTypeModelCode) {

        try {

            Wrapper<Long> wrapper = omcExpenseFeignApi.saveOmcExpenseTypeModelCode(omcExpenseTypeModelCode);

            if (wrapper == null) {
                throw new DccBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DccBizException(ErrorCodeEnum.MDC10021002);
            }

            Long result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("getDeviceById = {}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public Integer movePeopleTypeSubstractRate(List<MoveOldmanTypeIetype> ntOldmanTypeIetypeList) {
        try {
            Wrapper<Integer> wrapper = omcDataMoveFeighApi.movePeopleTypeSubstractRate(ntOldmanTypeIetypeList);

            if (wrapper == null) {
                throw new DccBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DccBizException(ErrorCodeEnum.MDC10021002);
            }
            Integer result = wrapper.getResult();
            return result;
        } catch (Exception e) {
            log.error("movePeopleTypeSubstractRate = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Integer initExpenseConfig(List<Long> groupIdList) {
        try {
            Wrapper<Integer> wrapper = omcDataMoveFeighApi.initExpenseConfig(groupIdList);

            if (wrapper == null) {
                throw new DccBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DccBizException(ErrorCodeEnum.MDC10021002);
            }
            Integer result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("initExpenseConfig = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * save omc expense item
     *
     * @param omcExpenseItem the omc expense item
     * @return
     */
    @Override
    public Long saveOmcExpenseItem(OmcExpenseItem omcExpenseItem) {

        try {

            Wrapper<Long> wrapper = omcExpenseFeignApi.saveOmcExpenseItem(omcExpenseItem);

            if (wrapper == null) {
                throw new DccBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DccBizException(ErrorCodeEnum.MDC10021002);
            }

            Long result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("saveOmcExpenseItem = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Integer moveOldmanIn(MoveOldmanIeType oldmanIeType) {
        try {
            Wrapper<Integer> wrapper = omcDataMoveFeighApi.moveOldmanIn(oldmanIeType);
            if (wrapper == null) {
                throw new DccBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DccBizException(ErrorCodeEnum.MDC10021002);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("moveOldmanIn = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Integer moveOldmanInItem(MoveOldmanIeType oldmanIeType) {
        try {
            Wrapper<Integer> wrapper = omcDataMoveFeighApi.moveOldmanInItem(oldmanIeType);
            if (wrapper == null) {
                throw new DccBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DccBizException(ErrorCodeEnum.MDC10021002);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("moveOldmanInItem = {}", e.getMessage(), e);
        }
        return null;
    }
}
